package com.example.dbcms.config;

import com.example.dbcms.service.TimedTaskService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.config.TriggerTask;
import org.springframework.scheduling.support.CronTrigger;

import java.util.Date;

@Api("定时器配置")
@Configuration
@EnableScheduling
public class ScheduleConfig implements SchedulingConfigurer {

    @Autowired
    private TimedTaskService timedTaskService;

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {

        taskRegistrar.addTriggerTask(new TriggerTask(new Runnable() {

            @Override
            public void run() {
                //具体调度的任务
                //System.out.println("定时器触发-----------------------");
                timedTaskService.refreshParkingFee();
                timedTaskService.refreshPropertyFee();
            }
        }, triggerContext -> {
            // 定义定时器的Cron间隔
           String cron = "0 0 8 1 * ?";//每月1号8点触发
            //String cron = "0 * * * * ?";//每隔一分钟触发一次
            //调用执行器
            return new CronTrigger(cron).nextExecutionTime(triggerContext);
        }));

    }
}
